package com.moetube.video.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.moetube.video.dao.CommentMapper;
import com.moetube.video.pojo.Barrage;
import com.moetube.video.pojo.Comment;
import com.moetube.video.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Comment> findAll() {
        return commentMapper.selectAll();
}

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Comment findById(Long id){
        return  commentMapper.selectByPrimaryKey(id);
    }


    /**
     * 增加
     * @param Comment
     */
    @Override
    public void add(Comment Comment){
        commentMapper.insert(Comment);
    }


    /**
     * 修改
     * @param Comment
     */
    @Override
    public void update(Comment Comment){
        commentMapper.updateByPrimaryKey(Comment);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        commentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<Comment> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return commentMapper.selectByExample(example);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Comment> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Comment>)commentMapper.selectAll();
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Comment> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Comment>)commentMapper.selectByExample(example);
    }

//    /**
//     * 根据商品分类名称查询品牌列表
//     * @param categoryname
//     * @return
//     */
//    @Override
//    static List<Map> findListByCategoryName(String categoryname) {
//        return commentMapper.findListByCategoryName(categoryname);
//    }

    @Override
    public List<Comment> findList(String videoId) {


        return commentMapper.findListByVideoId(videoId);
    }
    @Override
    public List<Comment> findList(int size,int page,Long videoId) {
        PageHelper.startPage(size, page);
        return (Page<Comment>)commentMapper.selectAll();
    }
    /**
     * 查询视频评论总数
     * @param id
     * @return
     */
    @Override
    public int findCommmentById(Long id) {
        int commentTotal = commentMapper.findCommentTotal(id);
        return commentTotal;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 品牌名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 品牌图片地址
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
           	}
            // 品牌的首字母
            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
                criteria.andLike("letter","%"+searchMap.get("letter")+"%");
           	}

            // 品牌id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // 排序
            if(searchMap.get("seq")!=null ){
                criteria.andEqualTo("seq",searchMap.get("seq"));
            }

        }
        return example;
    }

}
